import { Component } from 'react';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { View, Image, Text, Swiper, SwiperItem } from '@tarojs/components'
import { AtIcon, AtModal, AtDrawer } from 'taro-ui'
import {
  compareUserByUnionId
} from '@/api/cheap'
import { createCart } from '@/api/goods'
import { loginMethod } from '@/utils/login'
import './index.scss'
import dayjs from 'dayjs'
interface State {
  storeCode: '',
  pMobile: '',
  ppMobile: '',
  loadng: true,
}

export default class CheapOrderIndex extends Component<any, any> {
  $instance = getCurrentInstance();
  constructor (props) {
    super(props)
    this.state = {
      storeCode: '',
      pMobile: '',
      ppMobile: '',
      loadng: true,
    }
  }

  state: State

  async componentDidMount() {
    console.log('集划算h5', this.$instance.router.params)
    let { wxUnionid } = this.$instance.router.params
    Taro.showLoading({
      title: '加载中...',
      mask: true
    })
    if (wxUnionid) {
      compareUserByUnionId({ unionId: wxUnionid }).then(res => {
        let data = res.data.data
        if (!data.status) {
          console.log(data)
          loginMethod()
        } else {
          this.createCartOrder()
        }
      })
    }

  }

  getCurrentDate() {
    let date = new Date()
    return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`
  }

  createCartOrder() {
    let { jihuasuanId, pUnionId, ppUnionId, store, cartData, jihuasuanGoodsId, jihuasuanSource, pMobile, ppMobile, type, annualCardId, storeCode, thirdPartySource, thirdPartyShareSource } = this.$instance.router.params
    if (thirdPartySource) {
      Taro.setStorageSync('THIRD_PARTY_SOURCE', String())
    }
    if (cartData) {
      cartData = JSON.parse(cartData)
    }
    if (pUnionId || ppUnionId || pMobile || ppMobile || thirdPartyShareSource) { // 如果没有带进来的话，去拿缓存的，缓存的超过两天就自动失效，清空
      Taro.setStorageSync('CHEAP_DATA_MINI', {
        pMobile: pMobile || '',
        ppMobile: ppMobile || '',
        jihuasuanId: jihuasuanId || '',
        storeCode: storeCode || '',
        t: this.getCurrentDate(),
        ppUnionId: ppUnionId || '',
        pUnionId: pUnionId || '',
        thirdPartyShareSource: thirdPartyShareSource || ''
      })
    } else {
      let cheapData = Taro.getStorageSync('CHEAP_DATA_MINI')
      let current = dayjs(this.getCurrentDate())
      if (cheapData && cheapData.t) {
        let isEffect = current.diff(cheapData.t, 'day')
        if (isEffect <= 2) {
          pUnionId = cheapData.pUnionId
          ppUnionId = cheapData.ppUnionId
          pMobile = cheapData.pMobile
          ppMobile = cheapData.ppMobile
          thirdPartyShareSource = cheapData.thirdPartyShareSource
        } else { // 失效
          Taro.setStorageSync('CHEAP_DATA_MINI', '')
        }
      }
    }

    console.log('集划算h5 type', type)
    if (type && Number(type) == 1) {
      let url = `/pages/order/index/index?annualCardId=${annualCardId}&type=1&jihuasuanId=${jihuasuanId}${pMobile ? `&pMobile=${pMobile}` : ''}${ppMobile ? `&ppMobile=${ppMobile}` : ''}${pUnionId ? `&pUnionId=${pUnionId}` : ''}${ppUnionId ? `&ppUnionId=${ppUnionId}` : ''}&jihuasuanGoodsId=${jihuasuanGoodsId}&jihuasuanSource=0${storeCode ? `&storeCode=${storeCode}` : ''}${thirdPartyShareSource ? `&thirdPartyShareSource=${thirdPartyShareSource}` : ''}`
      Taro.hideLoading()
      Taro.redirectTo({ url })
    } else {
      createCart(cartData).then(res => {
        const data = res.data.data
        let url = `/pages/order/index/index?cartIds=${JSON.stringify(data.cartIds)}&store=${store}&jihuasuanId=${jihuasuanId}${pMobile ? `&pMobile=${pMobile}` : ''}${ppMobile ? `&ppMobile=${ppMobile}` : ''}${pUnionId ? `&pUnionId=${pUnionId}` : ''}${ppUnionId ? `&ppUnionId=${ppUnionId}` : ''}&jihuasuanGoodsId=${jihuasuanGoodsId}&jihuasuanSource=${jihuasuanSource}${thirdPartyShareSource ? `&thirdPartyShareSource=${thirdPartyShareSource}` : ''}`
        Taro.hideLoading()
        Taro.redirectTo({ url })
      })
    }

  }

  render() { }
}
